package com.example.certificate.controller;


import com.example.certificate.annoatiton.CurrentUser;
import com.example.certificate.annoatiton.LoginToken;
import com.example.certificate.dto.CardBasePageDto;
import com.example.certificate.dto.InsRoleDto;
import com.example.certificate.dto.common.DelByIdDto;
import com.example.certificate.dto.common.DisableStatDto;
import com.example.certificate.entity.SysRole;
import com.example.certificate.entity.User;
import com.example.certificate.mapper.SysRoleMapper;
import com.example.certificate.service.SysRoleService;
import com.example.certificate.utils.ResultConstant;
import com.example.certificate.utils.ResultEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author hao
 * @since 2022-02-23 17:13:12
 */
@RestController
@RequestMapping("/domain/sysRole")
@Api(tags = "系统角色表",value = "/domain/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;


    @LoginToken
    @ApiOperation(value = "新赠用户角色")
    @RequestMapping(value = "/insRole",method = RequestMethod.POST)
    public ResultEntity insRole(@Validated(value = {InsRoleDto.RoleGroup.class}) @RequestBody InsRoleDto dto, @ApiIgnore @CurrentUser User user){
        return roleService.insRole(dto,user);
    }


    @LoginToken
    @ApiOperation(value = "新赠用户角色")
    @RequestMapping(value = "/editRole",method = RequestMethod.POST)
    public ResultEntity editRole(@RequestBody SysRole dto, @ApiIgnore @CurrentUser User user){

        boolean editResult = roleService.updateById(dto);
        if(editResult)
            return new ResultEntity(ResultConstant.SUCCESS);

        return new ResultEntity(ResultConstant.MSG_CODE_ERROR);
    }

    @LoginToken
    @ApiOperation(value = "树形展示用户角色信息")
    @RequestMapping(value = "/selRoleTree",method = RequestMethod.GET)
    public ResultEntity selRoleTree(){
        return  roleService.selRoleTree();
    }

    @LoginToken
    @ApiOperation(value = "分页查询用户角色")
    @RequestMapping(value = "/selRolePage", method = RequestMethod.POST)
    public ResultEntity selRolePage(@RequestBody CardBasePageDto dto){
        return new ResultEntity(roleService.selRolePage(dto));
    }

    @LoginToken
    @ApiOperation(value = "修改用户角色信息")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public ResultEntity updateStatus(@Validated(value = {DisableStatDto.disableStatus.class}) @RequestBody DisableStatDto dto){
        return new ResultEntity(roleService.updateStatus(dto));
    }

    @LoginToken
    @ApiOperation(value = "获取用户角色级联显示数据")
    @RequestMapping(value = "/cascadeRole", method = RequestMethod.GET)
    public ResultEntity cascadeRole(){
        return new ResultEntity(roleService.cascadeRole());
    }

    @LoginToken
    @ApiOperation(value = "删除用户角色信息")
    @RequestMapping(value = "/delRole", method = RequestMethod.POST)
    public ResultEntity delRole(@Validated(value = {DelByIdDto.delDataById.class}) @RequestBody DelByIdDto dto){
        if(roleService.removeById(dto.getId())){
            return new ResultEntity();
        }
        return new ResultEntity(ResultConstant.DELETE_FAIL);
    }
}

